import router from './router'
import store from './store'
//Progress 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { ElMessage } from 'element-plus'

import {getToken} from "./utils/auth";
const whiteList = ['/login']

router.beforeEach((to,from,next)=>{
  NProgress.start()
  if(getToken()){
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      //处理角色权限相关
      // if (store.getters.roles.length === 0) {
      //   store.dispatch('GetInfo').then(res => { // 拉取用户信息
      //     let menus=res.data.menus;
      //     let username=res.data.username;
      //     store.dispatch('GenerateRoutes', { menus,username }).then(() => { // 生成可访问的路由表
      //       router.addRoute(store.getters.addRouters); // 动态添加可访问路由表 router@4 addRouter已弃用
      //       next({ ...to, replace: true })
      //     })
      //   }).catch((err) => {
      //     store.dispatch('FedLogOut').then(() => {
      //       ElMessage.error(err || 'Verification failed, please login again')
      //       next({ path: '/' })
      //     })
      //   })
      // } else {
      //   next()
      // }
    }
  }else{
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})


router.afterEach(()=>{
  NProgress.done()
})
